﻿using System.Windows;
using Jacobi.Seesmic.FeedReader.Model;
using Seesmic.Sdp.Utils;

namespace Jacobi.Seesmic.FeedReader.PluginSettingsView
{
    /// <summary>
    /// The view model for the settings view of the plugin.
    /// </summary>
    public sealed class PluginSettingsViewModel : ObservableObject
    {
        /// <summary>
        /// Constructs a new view model based on the <paramref name="feedInfos"/>.
        /// </summary>
        /// <param name="feedInfos">Must not be null.</param>
        internal PluginSettingsViewModel(FeedInfoCollection feedInfos)
        {
            FeedInfos = feedInfos;
        }

        /// <summary>
        /// Gets the list of feeds.
        /// </summary>
        public FeedInfoCollection FeedInfos { get; private set; }

        private FeedInfo _selectedItem;

        /// <summary>
        /// Gets or sets the selected feed.
        /// </summary>
        public FeedInfo SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        private DelegateCommand _addCommand;

        /// <summary>
        /// Gets the Add command that is bound to the view.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "Seesmic.Sdp.Utils.DelegateCommand.set_Text(System.String)", Justification = "No localization yet.")]
        public DelegateCommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(OnAddClick);
                    _addCommand.Text = "Add...";
                }

                return _addCommand;
            }
        }

        /// <summary>
        /// The handler for the Add command.
        /// </summary>
        public void OnAddClick()
        {
            var vwModel = new EditFeedInfoViewModel(FeedInfos);
            vwModel.ShowView();
        }

        private DelegateCommand _editCommand;

        /// <summary>
        /// Gets the Edit command that is bound to the view.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "Seesmic.Sdp.Utils.DelegateCommand.set_Text(System.String)", Justification = "No localization yet.")]
        public DelegateCommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(OnEditClick);
                    _editCommand.Text = "Edit...";
                }

                return _editCommand;
            }
        }

        /// <summary>
        /// The handler that is called for the Edit command.
        /// </summary>
        public void OnEditClick()
        {
            if (SelectedItem == null)
            {
                MessageBox.Show("Please select an item first.");
            }
            else
            {
                var vwModel = new EditFeedInfoViewModel(FeedInfos);
                vwModel.EditInfo = SelectedItem;

                vwModel.ShowView();
            }
        }

        private DelegateCommand _removeCommand;

        /// <summary>
        /// Gets the remove command that is bound to the view.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "Seesmic.Sdp.Utils.DelegateCommand.set_Text(System.String)", Justification = "No localization yet.")]
        public DelegateCommand RemoveCommand
        {
            get
            {
                if (_removeCommand == null)
                {
                    _removeCommand = new DelegateCommand(OnRemoveClick);
                    _removeCommand.Text = "Remove";
                }

                return _removeCommand;
            }
        }

        /// <summary>
        /// The handler that is called for the Remove command.
        /// </summary>
        public void OnRemoveClick()
        {
            if (SelectedItem == null)
            {
                MessageBox.Show("Please select an item first.");
            }
            else
            {
                FeedInfos.Remove(SelectedItem);
                SelectedItem = null;
            }
        }
    }
}